<?php
	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	 *
	 *	Autocredit
	 *	Copyright (C) 2012, Brent Flap <blog.cyberdevil.nl>
	 *
	 *	This file is part of Autocredit.
	 *
	 *	Autocredit is free software: you can redistribute it and/or modify
	 *	it under the terms of the GNU General Public License as published by
	 *	the Free Software Foundation, either version 3 of the License, or
	 *	any later version.
	 *
	 *	This program is distributed in the hope that it will be useful,
	 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
	 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 *	GNU General Public License for more details.
	 *
	 *	You should have received a copy of the GNU General Public License
	 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 *
	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	class nucash extends Plugins{
		private static $patterns = array(
			"/<h1>Ongelezen <strong>e-mails<\/strong><\/h1>(.*?)<h1>Gelezen/si",
			"/emaillezen.php\?lees=([0-9]*)/i",
			"/https:\/\/www.nucash.nl\/mailklik.php\?userid=(([0-9]*)(.*?))\">/i",
			"/Uitloggen/i",
			"/Welkom <strong>(.*?)<\/strong>/i",
			"/Mijn saldo:<\/span> <strong>&euro; ([0-9,]*)<\/strong>/i",
			"/<h1>([a-zA-Z!? ]*) - <strong>(.*?)\n(.*?)\n(.*?)betaaldebanners.php\?click=([0-9]*)&code=([a-z]*)\"/si",
			"/Wil je extra geld verdienen, dan kun je hieronder op de betaalde banners klikken.(.*?)<span class=\"bottom\"/si",
			"/https:\/\/www.nucash.nl\/mailo(.*?).jpg/i"
		);
		private static $plugin_name = "NuCash";
		private static $cookie_name = "nucash.cookie";
		private static $cert_name = "nucash.crt";
		function init(){
			message(self::$plugin_name . " loaded","COMMENT");
			return true;
		}
		function login(){
			$postfields = "login_username=".NUCASH_USER."&login_password=".NUCASH_PASS;
			$curl_options = array(
				CURLOPT_POST 		=> true,
				CURLOPT_POSTFIELDS	=> $postfields,
				CURLOPT_COOKIEJAR	=> DATA_PATH.self::$cookie_name, // Write the cookie
				CURLOPT_RETURNTRANSFER	=> true,
				CURLOPT_FOLLOWLOCATION	=> 1,
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			);
			$login = self::getPage("https://www.nucash.nl/members.php", $curl_options);
			if(self::Parse(self::$patterns[3], $login, 0) == "Uitloggen"){
				message("Succesfully signed in as ".self::Parse(self::$patterns[4], $login, 1).".","NOTICE");
				message("Credit: ".self::Parse(self::$patterns[5], $login, 1),"NOTICE");
			}
		}
		function betaald_klikken(){
			message("Betaald klikken", "COMMENT");
			$curl_options = array(
				CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name,
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			);
			$content = self::getPage("https://www.nucash.nl/betaaldebanners.php",$curl_options);
			foreach(explode("</a>",self::Parse(self::$patterns[7],$content,1)) as $add){
				$title = self::Parse(self::$patterns[6],$add,1);
				if(strlen($title) > 3){
					$final = "https://www.nucash.nl/betaaldebanners.php?click=".self::Parse(self::$patterns[6],$add,5)."&code=".self::Parse(self::$patterns[6],$add,6);
					$curl_options = array(
						CURLOPT_FOLLOWLOCATION	=> true,
						CURLOPT_RETURNTRANSFER	=> true,
						CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name,
						CURLOPT_SSL_VERIFYHOST => 0,
						CURLOPT_SSL_VERIFYPEER => 0
					);
					self::getPage($final,$curl_options);
					message("Clicked on: ".$title, "NOTICE");
				}
			}
		}
		function emails_lezen(){
			$counter = 0;
			message("Emails lezen", "COMMENT");
			$trigger = false;
			$curl_options = array(
				CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name,
				CURLOPT_SSL_VERIFYHOST => 0,
				CURLOPT_SSL_VERIFYPEER => 0
			);
			$content = self::getPage("https://www.nucash.nl/emaillezen.php",$curl_options);
			$unreaded_mails = self::Parse(self::$patterns[0],$content,1);
			$links = explode("</tr>",$unreaded_mails);
			if(count($links)>3){
				foreach($links as $l){
					if($links[0]!=$l && $links[count($links)-1]!=$l){
						$mail_id = self::Parse(self::$patterns[1],$l,1);
						$mail_title = self::Parse("/".$mail_id."\" target=\"_blank\">(.*?)<\/a>/i",$l,1);
						$curl_options = array(
							CURLOPT_COOKIEFILE	=> DATA_PATH.self::$cookie_name, // Read the cookie
							CURLOPT_SSL_VERIFYHOST => 0,
							CURLOPT_SSL_VERIFYPEER => 0
						);
						$content = self::getPage("https://www.nucash.nl/emaillezen.php?lees=".$mail_id,$curl_options);
						$hash_stuff = self::Parse(self::$patterns[2],$content,1);
						$final_link = "https://www.nucash.nl/mailklik.php?userid=".$hash_stuff;
						$curl_options = array(
							CURLOPT_FOLLOWLOCATION	=> true,
							CURLOPT_COOKIEFILE 	=> DATA_PATH.self::$cookie_name,
							CURLOPT_SSL_VERIFYHOST => 0,
							CURLOPT_SSL_VERIFYPEER => 0
						);
						self::getPage($final_link,$curl_options); // Gain credits
						
						$mailojpg = self::Parse(self::$patterns[8],$content,0);
						self::getPage($mailojpg,array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false)); // Remove the mail from the list, yeah lol it's done by loading a jpg.
						
						$counter++;
						message($counter." Read email: ".$mail_id." ".$mail_title,"COMMENT");
						sleep(15);
						$trigger = true;
					}
				}
			}
			if($trigger == false){
				// No new mails
				message("No new emails.", "NOTICE");
			}
		}
		function action(){
				$curl_options = array(
					CURLOPT_COOKIEFILE => DATA_PATH.self::$cookie_name,
					CURLOPT_SSL_VERIFYHOST => 0,
					CURLOPT_SSL_VERIFYPEER => 0
				); // Read the cookie
				$trigger = false;
			
				$login = self::getPage("https://www.nucash.nl/members.php", $curl_options);
				if(self::Parse(self::$patterns[3], $login, 0) != "Uitloggen"){
					message("Signed out!","ERROR");
					self::login();
				}
				else if(self::Parse(self::$patterns[3], $login, 0) == "Uitloggen"){
					message("Succesfully signed in as ".self::Parse(self::$patterns[4], $login, 1).".","NOTICE");
					message("Credit: ".self::Parse(self::$patterns[5], $login, 1),"NOTICE");
				}
				self::emails_lezen();
				self::betaald_klikken();
			return true;
		}
	}
	
?>
